import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login/Login.vue'
import Home from '@/views/Home/Home.vue'

import Welcome from '@/views/Welcome/Welcome.vue'
import Users from '@/views/User/Users.vue'
import Rights from '@/views/Power/Rights.vue'
import Roles from '@/views/Power/Roles.vue'
import Cate from '@/views/Goods/Cate.vue'
import Params from '@/views/Goods/Params.vue'
import GoodsList from '@/views/Goods/List.vue'
import GoodsAdd from '@/views/Goods/Add.vue'
import Order from '@/views/Order/Order.vue'
import Report from '@/views/Report/Report.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    children: [
      { path: '', redirect: 'welcome' },
      { path: 'welcome', component: Welcome },
      { path: 'users', component: Users },
      { path: 'rights', component: Rights },
      { path: 'roles', component: Roles },
      { path: 'categories', component: Cate },
      { path: 'params', component: Params },
      { path: 'goods', component: GoodsList },
      { path: 'goods_add', component: GoodsAdd },
      { path: 'orders', component: Order },
      { path: 'reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 过载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  // 判断 tokenStr 是否为空
  if (!tokenStr) return next('/login')
  next()
})

export default router
